package restfuljobs

import org.springframework.dao.DataIntegrityViolationException
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import groovyx.net.http.Method
import groovy.util.slurpersupport.NodeChild
import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

class JobApplicationReviewController extends AbstractRestController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	JobApplicationReviewController() {
		path = 'jobApplicationReview'
	}
	
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
		def jobApplicationReviewInstanceList = []
		
		def userId = 0
		
		if (session.user) {
			userId = session.user.id
		} else {
			redirect(uri: '/')
			return
		}
				
        httpBuilder.get( path: path, query: [userId: userId],
			headers : [Accept : 'application/xml']) { resp, xml ->
			
			xml.jobApplicationReview.each {
				def jobApplicationReview = new JobApplicationReviewWrapper()
				jobApplicationReview.id = it.attributes()['id'].toLong()				
				jobApplicationReview.setFromXML(it)					
				jobApplicationReviewInstanceList.add(jobApplicationReview)
			}
		}
			
        [jobApplicationReviewInstanceList: jobApplicationReviewInstanceList, jobApplicationReviewInstanceTotal: jobApplicationReviewInstanceList.size()]
    }

    def create() {
		
		if (!params.id) {
			redirect(controller: 'jobApplication')
			return
		}
		
		def userId = 0
		
		if (session.user) {
			userId = session.user.id
		}
		
		def jobApplicationReviewInstance = new JobApplicationReview(params)
		def jobApplicationInstance
		
		httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = "jobApplication/$params.id"
			uri.query = [userId: userId]
			response.success = { resp, xml ->
			  xml.each {
				jobApplicationInstance = new JobApplicationWrapper()
				jobApplicationInstance.id = xml.attributes()['id']?.toLong()
				jobApplicationInstance.setFromXML(it)
			  }			  
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: "JobApplication"), params.id])
				redirect(action: "list")
				return
			}
		}
		
		jobApplicationReviewInstance.application = jobApplicationInstance
		
		def userInstanceList = []
		
		httpBuilder.get( path: 'user', query: [userType: 'REVIEWER'],
			headers : [Accept : 'application/xml']) { resp, xml ->
			
			xml.user.each {
				def user = new UserWrapper()
				user.id = it.attributes()['id'].toLong()
				user.setFromXML(it)
				userInstanceList.add(user)
			}
		}
				
        [jobApplicationReviewInstance: jobApplicationReviewInstance, userList: userInstanceList]
    }

    def show() {
        def jobApplicationReviewInstance
		
		def userId = 0
		
		if (session.user) {
			userId = session.user.id
		}
		
		httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = path + "/$params.id"			
			uri.query = [userId: userId]
			
			response.success = { resp, xml ->
			  xml.each {
				jobApplicationReviewInstance = new JobApplicationReviewWrapper()
				jobApplicationReviewInstance.id = xml.attributes()['id']?.toLong()
				jobApplicationReviewInstance.setFromXML(it)
			  }			  
			  return [jobApplicationReviewInstance: jobApplicationReviewInstance]
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: path.capitalize()), params.id])
				redirect(action: "list")
				return
			}
		}     
    }

    def edit() {
        def jobApplicationReviewInstance
		
		def userId = 0
		
		if (session.user) {
			userId = session.user.id
		}
		
		httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = path + "/$params.id"			
			uri.query = [userId: userId]
			response.success = { resp, xml ->
			  xml.each {
				jobApplicationReviewInstance = new JobApplicationReviewWrapper()
				jobApplicationReviewInstance.id = xml.attributes()['id']?.toLong()
				jobApplicationReviewInstance.setFromXML(it)
			  }
			  
			  def userInstanceList = []
			  
			  httpBuilder.get( path: 'user', query: [userType: 'REVIEWER'],
				  headers : [Accept : 'application/xml']) { uresp, uxml ->
				  
				  uxml.user.each {
					  def user = new UserWrapper()
					  user.id = it.attributes()['id'].toLong()
					  user.setFromXML(it)
					  userInstanceList.add(user)
				  }
			  }
				  
			  return [jobApplicationReviewInstance: jobApplicationReviewInstance, userList: userInstanceList]
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: path.capitalize()), params.id])
				redirect(action: "list")
				return
			}
		}  
    }    
}
